<template>
  <div>
    <div class="CateC">
      <div class="CateC_L">
        <el-image class="CateC_L_img" :src="book.imgSrc" fit="fill"/>
      </div>
      <div class="CateC_R">
        <span class="book_name">{{book.name}}</span>
        <span class="author">作者： {{book.author}}</span>
         <span class="author">位置： {{book.bookshelfNum}}</span>
        <el-rate
                class="grade"
                v-model="Score"
                disabled
                show-score
                text-color="#ff9900"
                score-template="{value}">
        </el-rate>
        <span class="presentation">介绍： {{book.introduce}}</span>
      </div>
    </div>
    <el-button class="CateC_L_but" type="primary" @click="open(book.id)" :disabled="book.num <= 0">{{book.num <= 0 ? '数量不足':'借阅'}}</el-button>
  </div>
</template>

<script>
  import {apibookId,apiBorrow} from "../request/api"

  export default {
    name: "DetailsTop",
    data() {
      return {
        bookId: 1,
        book: "",
        Score: 0
      }
    },
    created() {
      this.$bus.on("bookId", (bookId) => {
        this.bookId = bookId;
        // console.log("bookId"+bookId)
      });
    },
    activated () {
      this.$bus.on("bookId", (bookId) => {
        this.bookId = bookId;
        console.log("bookId"+bookId)
      });
      // console.log("----------")
      apibookId({
        bookId: this.bookId
      }).then(res => {
        this.book = res.data.book
        this.Score = res.data.book.bookScore
            console.log(res)
      })
    },
    methods: {
      // 弹窗
      open(bookId) {
        if (this.$store.state.userId === '') {
          this.$message({
            message: '请先登录',
            type: 'warning'
          });
          this.$router.push('/login')
        }else {
          this.$confirm('你将借阅该书籍，请携带校卡前往图书馆取书，并与管理员确认。', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            apiBorrow({
              bookId: this.bookId,
              userId: this.$store.state.userId
            }).then(
                this.$message({
                  type: 'success',
                  message: '借阅成功!'
                })
            )
            this.$router.push('/My')
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消借阅'
            });
          });
        }
      }
    }
  }
</script>

<style scoped>
  .CateC {
    display: flex;
    flex-direction: row;
    margin: 50px 0 60px;
  }
  .CateC_L {
    width: 30%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .CateC_L_img {
    width: 236px;
    height: 236px;
  }
  .CateC_L_but {
    width: 20%;
    height: 50px;
    margin: 20px 40% 20px;
    font-size: 20px;
    text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  }
  .CateC_R {
    width: 65%;
    margin-top: 30px;
    margin-left: 5%;
    display: flex;
    flex-direction: column;
  }
  .book_name {

  }
  .author {
    margin-bottom: 10px;
  }
  .grade {
    margin-bottom: 10px;
  }
  .presentation {
    margin-right: 10%;
    line-height: 1.6;
  }
  .link {

  }

</style>
